//
//  TalkfunTestPaperDetailsTailView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/1/11.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TalkfunTestPaperDetailsTailView: UIView {
    //上一页
    @objc public let previousBtn = TalkfunIncreaseButton.init()
    //更多
    @objc public let more:TalkfunIncreaseButton = TalkfunIncreaseButton.init()
    //下一页
    @objc public let nextBtn = TalkfunIncreaseButton.init()

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.addSubview(self.previousBtn)
        self.addSubview(self.nextBtn)
        self.previousBtn.setImage(UIImage.init(named: "HTLive.bundle/TFicon_Previous"), for: UIControl.State.normal)
        self.nextBtn.setImage(UIImage.init(named: "HTLive.bundle/TFicon_next"), for: UIControl.State.normal)
        
        self.addSubview(self.more)
        self.more.setImage(UIImage.init(named: "HTLive.bundle/TFMore"), for: UIControl.State.normal)
        self.more.setImage(UIImage.init(named: "HTLive.bundle/TFMore"), for: UIControl.State.highlighted)
    }
    @objc func moreTitle(title:NSString)
    {
        var font = UIFont(name: "PingFang SC", size: 14) as Any
        //containsString
        //存在
        if (title.contains("/"))
       {
            font = UIFont(name: "PingFang SC", size: 15) as Any
           let attrString = NSMutableAttributedString(string: title as String)
           let attr: [NSAttributedString.Key : Any] = [.font: font,.foregroundColor: UIColor(red: 0.59, green: 0.63, blue: 0.67,alpha:1), ]
           attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
           
          let array =  attrString.mutableString.components(separatedBy: "/")
          let inx0 = array[0] as NSString
          let inx1 = array[1] as NSString
           
           let strSubAttr1: [NSMutableAttributedString.Key: Any] = [.font: font,.foregroundColor: UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1)]
           attrString.addAttributes(strSubAttr1, range: NSRange(location: 0, length: inx0.length))
           
           let strSubAttr2: [NSMutableAttributedString.Key: Any] = [.font: font,.foregroundColor: UIColor(red: 0.59, green: 0.63, blue: 0.67,alpha:1)]
           attrString.addAttributes(strSubAttr2, range: NSRange(location: inx0.length, length: inx1.length))
            self.more.setAttributedTitle(attrString, for: UIControl.State.normal)
       }
        else{
           //已交卷 或者 已批阅
           let attrString = NSMutableAttributedString(string: title as String)
               font = UIFont(name: "PingFang SC", size: 14) as Any
          var color = UIColor(red: 0.54, green: 0.61, blue: 0.69,alpha:1)
           if(title.contains("答对"))
           {
               color = UIColor(red: 0.05, green: 0.73, blue: 0.25,alpha:1)
           }
           let attr: [NSAttributedString.Key : Any] = [.font: font,.foregroundColor: color, ]
           attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
       
           self.more.setAttributedTitle(attrString, for: UIControl.State.normal)

       }
        
        
        let size: CGSize = title.size(withAttributes: [NSAttributedString.Key.font: font])
        
//        let moreY = 14.0
        let moreW = size.width + 22
//        let moreX = (self.frame.size.width - moreW)/2.0
        let moreH = 21.0
        self.more.spacing = 2
        self.more.bounds = CGRect(x: 0, y: 0, width: moreW, height: moreH)

    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.previousBtn.frame = CGRect(x: 48.0, y: 14.0, width: 32.0, height: 32.0)
        self.nextBtn.frame = CGRect(x: self.frame.width - 48.0 - 32.0, y: 14.0, width: 32.0, height: 32.0)
        let moreY = 14.0
        self.more.center = CGPoint(x: self.frame.width / 2.0, y: self.frame.height / 2.0 - moreY)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
